﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Mvc.Utility; 
using Wedo.Recognition.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Web.Controllers;
using Wedo.Recognition.Service;
using Wedo.Mvc.Pager;
using Wedo.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Repository;

namespace Wedo.IPRP.Web.Controllers
{
    public class TransferVoucherController : EntityController<TransferVoucher, CommonTimeSearch>
    {
        private BankDataService dbService;
        private TransferVoucherService service;
        public TransferVoucherController(IUnitWork dbContext)
            : base(dbContext)
        {
            dbService = new BankDataService(_dbContext);
            service = new TransferVoucherService(_dbContext);
        }

        public override ActionResult Index()
        {
            ViewBag.HasConfirm = false;
            return base.Index();
        }

        [HttpPost]
        public ActionResult BatchEdit(string id, FormCollection collection)
        {
            ResultMSG res = new ResultMSG();
            if (string.IsNullOrWhiteSpace(id))
            {
                res.Result =false;
                res.MSG = "请选择需要更新的行";
                return Json(res);
            }
            try
            {
                service.TransferVoucherConfirm(id);
                res.Result =true;
                res.MSG = "转账凭证确认成功";
            }
            catch (Exception ex)
            {
                res.Result =false;
                res.MSG = ex.Message;
            }

            return Json(res);
        }

        protected override Wedo.Service.Common.IBaseService<TransferVoucher> EntityService
        {
            get { return service; }
        }

        protected override PagedList<TransferVoucher> GetSearchPagedList(CommonTimeSearch where)
        {
            where = where ?? new CommonTimeSearch();

            var query = service.All();
            if (where.StartTime.HasValue)
            {
                query = query.Where(m => m.Created > where.StartTime);
            }

            if (where.EndTime.HasValue)
            {
                DateTime endTime = where.EndTime.Value.AddDays(1);
                query = query.Where(m => m.Created < endTime);
            }
            if (where.Keyword.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.Assiginment.Contains(where.Keyword));
            }

            return query.OrderByDescending(m => m.Created)
                .ToPagedList(1, 10);
        }
    }
}
